﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;

namespace IntroduceCompany.Models
{
    public class BossEmailModels
    {
        internal void ChangeActive(int id, bool active)
        {
            BossEmail obj = DataSingleton.GetInstance().BossEmails.SingleOrDefault(u => u.ID == id);
            obj.Active = (!active);
            DataSingleton.GetInstance().BossEmails.Context.SubmitChanges();
        }

        internal void CreateBossEmail(BossEmail bossEmail)
        {
            try
            {
                bossEmail.Active = false;
                bossEmail.Email = bossEmail.Email.Trim();
                bossEmail.Pass = Crypto.EncryptStringAES(bossEmail.Pass.Trim(),"AH5P");

                DataSingleton.GetInstance().BossEmails.InsertOnSubmit(bossEmail);
                DataSingleton.GetInstance().BossEmails.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        internal void EditBossEmail(BossEmail bossEmail)
        {
            try
            {
                BossEmail old = DataSingleton.GetInstance().BossEmails.SingleOrDefault(u => u.ID == bossEmail.ID);
                old.Email = bossEmail.Email.Trim();
                old.Pass = Crypto.EncryptStringAES(bossEmail.Pass.Trim(), ConstantClass._DecryptPass);
                old.SenderName = bossEmail.SenderName.Trim();
                old.SmtpServer = bossEmail.SmtpServer.Trim();
                old.Port = bossEmail.Port;

                DataSingleton.GetInstance().BossEmails.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        internal void DeleteBossEmail(int id)
        {
            BossEmail mail = DataSingleton.GetInstance().BossEmails.SingleOrDefault(u => u.ID == id);
            DataSingleton.GetInstance().BossEmails.DeleteOnSubmit(mail);
            DataSingleton.GetInstance().BossEmails.Context.SubmitChanges();
        }

        internal void ChangeSSL(int id, bool ssl)
        {
            BossEmail obj = DataSingleton.GetInstance().BossEmails.SingleOrDefault(u => u.ID == id);
            obj.SSL = (!ssl);
            DataSingleton.GetInstance().BossEmails.Context.SubmitChanges();
        }
    }
}